package com.ftg.learn.bus.sharecar_expendrecord.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftg.learn.bus.deptexam.mapper.DeptExamMapper;
import com.ftg.learn.bus.sharecar_expendrecord.mapper.Sharecar_Expendrecord_Admin_Mapper;
import com.ftg.learn.bus.sharecar_expendrecord.pojo.sharecar_expendrecord_admin;
import com.ftg.learn.bus.sharecarexp_record.service.SharecarExpRecordService;
import com.ftg.learn.bus.util.ResultType;
import com.ftg.learn.bus.util.ToExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class Sharecar_Expendrecord_Admin_Service {


    @Autowired
    Sharecar_Expendrecord_Admin_Mapper add;

    @Autowired
    SharecarExpRecordService scers;

    @Autowired
    DeptExamMapper dem;


    /**
     * 查询全部
     */
    public List<sharecar_expendrecord_admin> query() {
        return add.query();
    }

    /**
     * 根据id查询一条
     * id
     * @return
     */
    public sharecar_expendrecord_admin queryOne(String id) {
        if (id == null || id.equals("")) {
          return null;
        }
        return add.findOne(id);
    }

    /**
     * 添加
     * @param sua
     * @return
     */
    public int insert(sharecar_expendrecord_admin sua){
        if (sua.getExpendcar_id() ==null|| sua.getExpendcar_id().equals("") ||
                sua.getCar_id() ==null|| sua.getCar_id().equals("") ||
                sua.getApparatus_number() ==null|| sua.getApparatus_number().equals("") ||
                sua.getPrincipal() ==null|| sua.getPrincipal().equals("") ||
                sua.getRecipient() ==null|| sua.getRecipient().equals("")||
                sua.getInvoice_number() ==null|| sua.getInvoice_number().equals("") ||
                sua.getCost_type() ==null|| sua.getCost_type().equals("")||
                sua.getManufacturer() ==null|| sua.getManufacturer().equals("") ||
                sua.getCost_money() ==null|| sua.getCost_money().equals("") ||
                sua.getCheck_state() ==null|| sua.getCheck_state().equals("")) {
            return -1;
        }else if(add.findOne(sua.getExpendcar_id()) != null){
            return -1;
        }else {
            if(sua.getCheck_state().equals("特殊情况")){
                dem.insert(sua.getExpendcar_id());
            }
            scers.SharecarExpInsertRecord(sua.getCar_id(),sua.getPrincipal());
            return add.insert(sua);
        }
    }

    /**
     * 修改
     * @param scera
     * @param expendcar_id
     * @return
     */
    public int update(sharecar_expendrecord_admin scera, String expendcar_id) {
        if(queryOne(expendcar_id) == null || queryOne(expendcar_id).equals("")){
             return -1;
        }
        QueryWrapper qw = new QueryWrapper();
        qw.eq("expendcar_id",expendcar_id);
        scers.SharecarExpUpdateRecord(expendcar_id,scera.getPrincipal());
        return add.update(scera,qw);
    }

    /**
     *删除
     */
    public  int delete(String id){
        return add.deleteById(id);
    }

    /**
     * 分页查询
     * @param page        --页数
     * @param limit       --每页显示条目数
     * @param expendcar_id   --实体类主键  用于模糊查询
     * @param principal   --负责人     用于模糊查询
     * @return
     */
    public ResultType<sharecar_expendrecord_admin> query(int page, int limit, String expendcar_id, String principal) {
        ResultType<sharecar_expendrecord_admin> rs = new ResultType<>();
        rs.setCode(0);
        IPage<sharecar_expendrecord_admin> ip = new Page<>(page,limit);
        QueryWrapper<sharecar_expendrecord_admin> qw=new QueryWrapper<>();
        qw.and(wrapper->wrapper.like("expendcar_id",expendcar_id).like("principal",principal));

        ip =  add.selectPage(ip,qw);
        rs.setData(ip.getRecords());
        rs.setCount(ip.getTotal());
        return rs;
    }

    /**
     * 费用统计表
     * @return
     */
    public List<Map<String,Object>> costTable(){
        return add.costTable();
    }

    /**
     * 公车发票
     * @param car_id
     * @return
     */
    public List<Map<String,Object>> invoice(String car_id){
        return add.invoice(car_id);
    }


    /**
     * 导出为excel
     * @param url
     * @param
     */
    public String exportExcel(String url){
        if(url!=null || !url.equals("")){
            ToExcel t=new ToExcel();
            try {
                t.WriteExcel(url="","公车使用经费",add.find(),add.selectColumnNameAndComment());
                return "成功";
            }catch (Exception e){
                System.out.println("exception");
                return "失败";
            }
        }
        return "失败";
    }
}

